package com.mosai.smartmedical.service;

import android.app.Notification;
import android.os.Bundle;
import android.service.notification.NotificationListenerService;
import android.service.notification.StatusBarNotification;
import android.util.Log;

import com.mosai.smartmedical.entity.NotificationListenerEvent;

import org.greenrobot.eventbus.EventBus;

/**
 * Created by Rays on 2020/12/10.
 */
public class MyNotificationListenerService extends NotificationListenerService {
    public static final String TAG = MyNotificationListenerService.class.getSimpleName();

    @Override
    public void onListenerConnected() {
        super.onListenerConnected();
        Log.i(TAG, "onListenerConnected: ");
        EventBus.getDefault().post(new NotificationListenerEvent(NotificationListenerEvent.TYPE_CONNECTED));
    }

    @Override
    public void onListenerDisconnected() {
        super.onListenerDisconnected();
        Log.i(TAG, "onListenerDisconnected: ");
        EventBus.getDefault().post(new NotificationListenerEvent(NotificationListenerEvent.TYPE_DISCONNECTED));
    }

    @Override
    public void onNotificationPosted(StatusBarNotification sbn) {
        super.onNotificationPosted(sbn);
        Bundle extras = sbn.getNotification().extras;
        Log.i(TAG, "onNotificationPosted:"
                + " sbn: " + sbn
                + " title: " + extras.getString(Notification.EXTRA_TITLE)
                + " text: " + extras.getString(Notification.EXTRA_TEXT)
        );
        EventBus.getDefault().post(new NotificationListenerEvent(NotificationListenerEvent.TYPE_POSTED, sbn));
    }

    @Override
    public void onNotificationRemoved(StatusBarNotification sbn) {
        super.onNotificationRemoved(sbn);
        Bundle extras = sbn.getNotification().extras;
        Log.i(TAG, "onNotificationRemoved:"
                + " sbn: " + sbn
                + " title: " + extras.getString(Notification.EXTRA_TITLE)
                + " text: " + extras.getString(Notification.EXTRA_TEXT)
        );
        EventBus.getDefault().post(new NotificationListenerEvent(NotificationListenerEvent.TYPE_REMOVED, sbn));
    }
}
